<?php

namespace app\admin\controller;

use app\common\model\IconTypes;
use think\Controller;
use app\common\model\Tasks as M;

class Tasks extends Controller
{

    public function index()
    {
        $mtype = new IconTypes();
        $types = $mtype->getAllTypes();


        return $this->fetch('index', ['types' => $types,'my_self'=>session('my_self')]);
    }


    public function add()
    {
        $mtype = new IconTypes();
        $types = $mtype->getAllTypes();

        return $this->fetch('edit', ['types' => $types,'method' => 'add','my_self'=>session('my_self'),]);
    }


    public function edit()
    {

        $mtype = new IconTypes();
        $types = $mtype->getAllTypes();

        $m = new M();
        $s = $m->getById(input('get.id'));

        return $this->fetch('edit', ['types' => $types,'method' => 'edit', 's' => $s,'my_self'=>session('my_self'),]);
    }


    //获取日志josn数据
    public function get_json()
    {
        $m          = new M();
        $shopslist  = $m->getDatas('page'); //每页数据
        $shopscount = $m->getDatas(); //
        $count      = count($shopscount);
        $lists      = ['code' => 0, 'msg' => '', 'count' => $count, 'data' => []];

        $mtype = new IconTypes();
        $types = IconTypes::getNames();

        $round_types = round_types();
        foreach ($shopslist as $k => $v) {
            $v['is_used']     = $v['is_used'] == '1' ? '正常' : '隐藏';
            $v['machine_type_name'] = $types[$v['machine_type']];
            $v['round_name'] = $round_types[$v['round']];
            $lists['data'][] = $v;
        }
        return $lists;
    }


    /**
     * 提交数据
     */
    public function contents_submit()
    {

        $data        = input('post.');
        $m           = new M();
        $round_types = round_types();

        if ($data['method'] == 'add') {
            $data['create_id']   = $data['uid'];
            $data['create_user'] = $data['uname'];
            $data['create_at']   = T();
            $data['round_name']  = $round_types[$data['round']];
            $rs                  = $m->add($data);
        }
        else if ($data['method'] == 'edit') {
            $data['update_id']   = $data['uid'];
            $data['update_user'] = $data['uname'];
            $data['round_name']  = $round_types[$data['round']];
            $data['update_at']   = T();
            $rs                  = $m->modify($data, $data['id']);
        }

        return js_return($rs[0], $rs[1]);
    }


    /*
 * 删除
 *
 */

    public function del_submit()
    {
        $m      = new M();
        $result = $m->destroy(input('id'));

        if (false !== $result) {
            return js_return(1, "删除成功");
        }
        else {
            return js_return(0, $this->getError());
        }
    }

}
